$(document).ready (function() {
  var oAutoCompCustomer;
  var resultCallbackCustomer = function(sRequest, oResponse, oPayload) {
    myCustomerDataTable.onDataReturnInitializeTable(sRequest, oResponse, oPayload);
	myCustomerDataTable.selectRow(0);
  };
  
  var getCustomerResults = function(query, last) {
	  myDataSourceCustomer.sendRequest('query=' + query, 
	resultCallbackCustomer, myCustomerDataTable);
  };

  var oACDSCustomer = new YAHOO.widget.DS_JSFunction(getCustomerResults);
  oACDSCustomer.maxCacheEntries = 0;
  oAutoCompCustomer = new YAHOO.widget.AutoComplete("dt_input_customer","dt_ac_container_customer", oACDSCustomer);
  oAutoCompCustomer.minQueryLength = 0;
  var myColumnHeadersCustomer = [{key:"id", label: "id", sortable: false, height: '2em', width:'10%'}, 
	                             {key:"name", label: "name", sortable: false, height: '2em', width:'30%'},
	                             {key:"cuil", label: "cuil", sortable: false, height: '2em', width:'20%'},
	                             {key:"address", label: "address", sortable: false, height: '2em', width:'20%'},
	                             {key:"contact", label: "contact", sortable: false, height: '2em', width:'20%'}];
  var myColumnSetCustomer = new YAHOO.widget.ColumnSet(myColumnHeadersCustomer);
  var myDataSourceCustomer = new YAHOO.util.DataSource(baseweb + "/module/customer/searchCustomer.do?");
  myDataSourceCustomer.responseType = YAHOO.util.DataSource.TYPE_JSON;
  myDataSourceCustomer.maxCacheEntries = 50;
  myDataSourceCustomer.responseSchema = {
	          resultsList: "Result",
	          fields: ["id", "name", "cuil", "address", "contact"]
	        };
	
 var oConfigsCustomer = {
	          initialRequest: "query=", 
	          selectionMode:"single",
	          scrollable:true,
	          height:"20em",
	          width:"600px"
	        };
 function rowSelectedCustomer(oArgs) {
	          target = oArgs.target;
	          id = target.getElementsByClassName('yui-dt-col-id')[0].children[0].innerHTML;
	          name = target.getElementsByClassName('yui-dt-col-name')[0].children[0].innerHTML;
	          cuil = target.getElementsByClassName('yui-dt-col-cuil')[0].children[0].innerHTML;
	          addCustomerToResultCustomer(id, name, cuil);
	          myCustomerDataTable.onEventSelectRow(oArgs);
	          $('#myCustomerTable').toggle(); 
	        }
 YAHOO.widget.DataTable.MSG_LOADING = "Something";
 var myCustomerDataTable = new YAHOO.widget.DataTable("datatableCustomer", myColumnSetCustomer, myDataSourceCustomer, oConfigsCustomer);
 myCustomerDataTable.subscribe("rowMouseoverEvent", myCustomerDataTable.onEventHighlightRow);
 myCustomerDataTable.subscribe("rowMouseoutEvent", myCustomerDataTable.onEventUnhighlightRow);
 myCustomerDataTable.subscribe("rowClickEvent", rowSelectedCustomer);
 myCustomerDataTable.subscribe("renderEvent", function() {myCustomerDataTable.unselectAllRows();});
 myCustomerDataTable.selectRow(0);			        
	
 function addCustomerToResultCustomer(id, name, cuil) {
	 $('#customerId').val(id);
	 $('#customerName').val(name);
	 $('#cuil').val(cuil);
	 return false;
  }
	    
  var ddCustomer = new YAHOO.util.DD("CustomerTablePopUp");
  ddCustomer.setHandleElId("CustomerTableTitle");
  ddCustomer.addInvalidHandleType('input');
  $('#dt_input_customer').val('');
	      
  $('#activateCustomerTable').click(function() {
    $('#myCustomerTable').toggle();
	return false;
  });

  $('#closeCustomerTable').click(function() {
	$('#myCustomerTable').toggle(); 
    $('dt_input_customer').val('');
    return false;
  });
});
